/*
 * ModuleArrayDescription.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */
package engine;

import java.util.*;
import java.io.*;
import middle.*;


/**
 * This is the description of an array of module instances.
 */
class ModuleArrayDescription extends ModuleInstanceDescription {

	ExpressionDescription arrayStart, arrayEnd;


	/**
	 * TODO: this should tell whether aray's components should be
	 * placed into the watch tree (I don't think a 1024x1024 gate array
	 * is something Swing can handle into a tree ...)
	 */
	static boolean buildSubTree;

	ModuleArrayDescription(String name, int lineNo,
			ExpressionDescription arrayStart,
			ExpressionDescription arrayEnd,
			String descriptorName,
			byte strength,
			Delay3Description delays,
			Vector portsVector,
			Hashtable portsHash,
			Vector parameters){
		super(name, lineNo, descriptorName, null,
				strength, delays,
				portsVector, portsHash, parameters);
		this.arrayStart = arrayStart;
		this.arrayEnd = arrayEnd;
	}

	Object instantiate(NameSpace parent, ScopeNode thisScope)
	throws ParseException
	{
		resolveDescriptor(parent);

		int ars, are;
		try {
			ars = (int) arrayStart.instantiate(parent).evaluate().getLong();
			are = (int) arrayEnd.instantiate(parent).evaluate().getLong();
		} catch (InterpretTimeException iex) {
			throw new ParseException (((DefaultNameSpaceDescription) parent.desc).
					toString(lineNo) + iex);
		}
		int increment, n;
		increment = ars < are ? 1 : -1;
		n = Math.abs(ars-are) +1;

		Object o = ((ModuleFactory) descriptor).
		createNewModuleArray(parent,
				this, strength,
				thisScope.addScope(Constants.plaIcon, name),
				ars, are, n, increment);
		descriptor.unsetParams();
		return o;
	}

	public int getType() {
		return moduleArType;
	}

	public String toString(){
		return name;
	}

}

